package cn.kgc.controller.dict;

import cn.kgc.pojo.Dict;
import cn.kgc.pojo.DictMaxType;
import cn.kgc.service.system.DictMaxTypeService;
import cn.kgc.service.system.DictService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class dictController {

    @Resource
    public DictService dictService;
    @Resource
    public DictMaxTypeService dictMaxTypeService;

    /** 字典功能的首页
     * **/
    @RequestMapping("/home")
    public ModelAndView home(Model model){
        ModelAndView mv = new ModelAndView();
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        List<DictMaxType> list = dictMaxTypeService.selectDictMaxType();
        mv.addObject("dictTop",list);
        mv.setViewName("dict");
        return mv;
    }
    /** 子字典功能的首页
     * **/
    @RequestMapping("/dictsub")
    public ModelAndView dictsub(DictMaxType dictMaxType){
        ModelAndView mv = new ModelAndView();
        mv.addObject("dictMaxType",dictMaxType);
        //获取子字典
        List<Dict> list = dictService.selectDictByTypeId(dictMaxType.getId());
        mv.addObject("dictSub",list);
        mv.setViewName("dictsub");
        return mv;
    }
    /** 获取字典大类数据
     * **/
    @RequestMapping("/getDictMaxTypeList")
    @ResponseBody
    public String getDictMaxTypeList(){
        List<DictMaxType> list = dictMaxTypeService.selectDictMaxType();
        Map rstMap = new HashMap<>();
        //返回格式符合LayUi的要求
        rstMap.put("code",0);
        rstMap.put("msg","");
        rstMap.put("count",list != null ? list.size() : 0);
        rstMap.put("data",list);
        return JSON.toJSONString(rstMap);
    }
    /** 保存字典数据(大类)
     * **/
    @RequestMapping("/saveDictMaxType")
    @ResponseBody
    public String saveDictMaxType(DictMaxType dictMaxType){
        Map rstMap = new HashMap<>();

        int rst = dictMaxTypeService.addDictMaxType(dictMaxType);
        if(rst !=1 ){
            rstMap.put("resultCode",-1); //失败
            rstMap.put("msg","保存字典大类失败");
        }else{
            rstMap.put("resultCode",0); //失败
            rstMap.put("msg","保存字典大类成功！");
        }
        return JSON.toJSONString(rstMap);
    }

    /** 保存字典数据
     * **/
    @RequestMapping("/saveDict")
    @ResponseBody
    public String saveDict(Dict dict){
        Map rstMap = new HashMap<>();
        int rst = dictService.addDict(dict);
        if(rst !=1 ){
            rstMap.put("resultCode",-1); //失败
            rstMap.put("msg","保存字典失败");
        }else{
            rstMap.put("resultCode",0); //失败
            rstMap.put("msg","保存字典成功！");
        }
        return JSON.toJSONString(rstMap);
    }
    /** 删除字典数据
     * **/
    @RequestMapping("/delDict")
    @ResponseBody
    public String delDict(Dict dict){
        Map rstMap = new HashMap<>();
        int rst = dictService.deleteDict(dict.getId());
        if(rst !=1 ){
            rstMap.put("resultCode",-1); //失败
            rstMap.put("msg","删除字典失败");
        }else{
            rstMap.put("resultCode",0); //失败
            rstMap.put("msg","删除字典成功！");
        }
        return JSON.toJSONString(rstMap);
    }
    /** 删除字典大类。同时删除对应子字典。
     * **/
    @RequestMapping("/delDictMaxType")
    @ResponseBody
    public String delDictMaxType(DictMaxType dictMaxType){
        Map rstMap = new HashMap<>();
        int rst = dictMaxTypeService.deleteDictMaxType(dictMaxType.getId());
        if(rst !=1 ){
            rstMap.put("resultCode",-1); //失败
            rstMap.put("msg","删除字典大类失败");
        }else{
            rstMap.put("resultCode",0); //失败
            rstMap.put("msg","删除字典大类成功！");
        }
        return JSON.toJSONString(rstMap);
    }
    /** 根据大类，获取字典数据
     * **/
    @RequestMapping("/getDictListByTypeId")
    @ResponseBody
    public String getDictListByTypeId(Integer typeId){
        List<Dict> list = dictService.selectDictByTypeId(typeId);
        Map rstMap = new HashMap<>();
        //返回格式符合LayUi的要求
        rstMap.put("code",0);
        rstMap.put("msg","");
        rstMap.put("count",list != null ? list.size() : 0);
        rstMap.put("data",list);
        return JSON.toJSONString(rstMap);
    }
}
